import { ref } from 'vue'
import { defineStore } from 'pinia'
import { getRoomAPI,getRoomDevsAPI } from '@/apis/room.js'

export const useRoomStore = defineStore('room', () => {
    // state
    const roomInfo = ref([])

    //获取房间数据和设置user信息
    const getRoomData = async () => {
        // 获取全部房间信息
        const res = await getRoomAPI();
        roomInfo.value = res.data.data;
        
        // 获取各个房间对应的设备信息
        const promises = roomInfo.value.map(room => getRoomDevData(room.roomId));
        const roomDevsData = await Promise.all(promises);
        
        for (let i = 0; i < roomInfo.value.length; i++) {
            roomInfo.value[i].devs = roomDevsData[i];
        }
    }

    const getRoomDevData = async (roomId) => {
        const res = await getRoomDevsAPI(roomId);
        return res.data.data;
    }

    return {
        roomInfo,
        getRoomData,
    }
})